'use strict';

import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  indexAction(){
    //auto render template file index_index.html
    return this.display();
  }

  async dologinAction(){
  	let data = this.post();

  	let email = data.email;
    let md5Pas = await think.md5(data.password);
    console.log(md5Pas);
    let result=await this.model('home').findOne('sys_user',{email:email});
  	if (md5Pas === result.password && email === result.email) {
      await this.session("userInfo", {
        id: result.id,
        name: result.name,
        nickname: result.nickname,
        email: result.email,
        createtime: result.createtime,
      });
  		return this.success('登录成功');
  	}else{
  		return this.fail(1);
  	}

  }

  async loginoutAction(){
    await this.session();
    return this.success('退出成功')
  }

  async authAction(){
    let userInfo = await this.session('userInfo');
    return think.isEmpty(userInfo)? this.fail(5): this.success(userInfo);
  }
}